package com.cxsw.rms.controller;

import com.cxsw.rms.entity.Student;
import com.cxsw.rms.service.StudentService;
import com.cxsw.rms.utils.Constants;
import com.cxsw.rms.utils.DataUtils;
import com.cxsw.rms.utils.ResultObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;

/**
 * 学生控制器
 *
 * @author Han Fuhao
 * @date 2023/05/21
 */
@RestController
@RequestMapping("student")
public class StudentController {
    /**
     * 关于学生信息业务逻辑层Bean注入
     */
    @Resource
    private StudentService studentService;
    /**
     * 上传照片保存路径
     */
    @Value("${multipartFile.file.path}")
    private String upLoadPath;
    /**
     * 获取学生数据
     *
     * @return {@link ResultObject}
     */
    @RequestMapping("getStudent")
    public Object getStudent(String name, String phone, String sex, Integer pageNo, Integer pageSize){
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("phone", phone);
        map.put("sex", sex);
        map.put("pageNo", pageNo);
        map.put("pageSize", pageSize);
        return studentService.getStudentForPage(map);
    }

    /**
     * 通过学生学号获取学生信息
     *
     * @param stuId 学生id
     * @return {@link ResultObject}
     */
    @RequestMapping("getStudentByStuId")
    public Object getStudentByStuId(Integer stuId, HttpSession session){
        session.setAttribute(Constants.SESSION_STUDENT_ID, stuId);
        return studentService.getStudentByStuId(stuId);
    }

    /**
     * 修改学生信息
     *
     * @param student 学生
     * @return {@link Object}
     */
    @RequestMapping("updateStudent")
    public ResultObject updateStudent(@RequestBody Student student, HttpSession session) throws IOException {
        Integer stuId = (Integer) session.getAttribute(Constants.SESSION_STUDENT_ID);
        student.setBirthday(student.getBirthday());
        student.setCreateTime(DataUtils.formatDateTime(new Date()));
        student.setUpdateTime(DataUtils.formatDateTime(new Date()));
        return studentService.updateStudent(student, stuId, session);
    }

    /**
     * 学生img
     *
     * @param file    文件
     * @param session 会话
     * @return {@link ResultObject}
     * @throws IOException ioexception
     */
    @RequestMapping("updateStudentImg")
    public ResultObject updateStudentImg(@RequestBody MultipartFile file, HttpSession session) throws IOException {
        if (file != null){
            byte[] fileData = file.getBytes();
            session.setAttribute(Constants.SESSION_USER_IMG, fileData);
            return ResultObject.success("上传成功！", null);
        }
        else {
            return ResultObject.failure("上传失败！");
        }
    }

    @RequestMapping("updateImgToStudent")
    public void updateImgToStudent(@RequestBody Integer studentId, HttpSession session) throws IOException {
        studentService.updateStudentImg(studentId, session);
    }

    @RequestMapping("delStudent")
    public ResultObject delStudent(@RequestBody Integer studentId){
        return studentService.delStudent(studentId);
    }
}
